package com.dkha.server.controllers;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dkha.common.enums.YNEnums;
import com.dkha.common.exception.DkException;
import com.dkha.common.modules.vo.control.TaskControlVO;
import com.dkha.common.modules.vo.control.VideoControlVo;
import com.dkha.common.modules.vo.facelib.CompareRequestVo;
import com.dkha.common.modules.vo.facelib.FileImageVo;
import com.dkha.common.page.PageParam;
import com.dkha.common.result.CommonResult;
import com.dkha.common.systemcode.SystemCode;
import com.dkha.common.util.UtilValidate;
import com.dkha.server.annotation.NoRepeatSubmitAnnotation;
import com.dkha.server.modules.entities.*;
import com.dkha.server.services.*;
import com.dkha.server.system.common.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @version V1.0
 * @Description: 布控任务控制器
 * @Title:
 * @author: huangyugang
 * @date: 2019-12-09  10：25
 * @Copyright: 成都电科慧安
 */
@RestController
@RequestMapping("/control")
@Api(tags = "布控任务管理")
@Slf4j
public class ControlTaskController extends CommonResult {

    @Autowired
    IControlTaskService iControlTaskService;
    @Autowired
    IControlLibraryMidService iControlLibraryMidService;
    @Autowired
    IControlBayonetMidService iControlBayonetMidService;
    @Autowired
    private ICompareService compareService;

    @ApiOperation(value = "获取布控任务列表")
    @PostMapping(value = "/tasklist")
    public CommonResult getTaskList(@ApiParam(required = true, name = "pageParam", value = "{\n" +
            "  \"note\": {\n" +
            "  \"taskName\": \"任务名称\",\n" +
            "  \"createBy\":\"创建人\",\n" +
            "  \"disposalType\":\" 处置类型-码值视频对比 -1 抓捕类1，监控类2，提示类0 默认选择0\",\n" +
            "  \"controlType\":\"布控类型-码值 长期布控 0，临时布控 1 默认选择0\",\n" +
            "  \"isValid\":\"运行中:0 停止运行-1 暂停运行 1\",\n" +
            "  },\n" +
            "  \"pageNo\": 1,\n" +
            "  \"pageSize\": 10,\n" +
            "}") @RequestBody PageParam pageParam, BindingResult bindingResult) {
        String disposetype=   pageParam.getNote().get("disposalType");
        Page<ControlTask> controlTask=null;
        if(!StringUtils.isEmpty(disposetype)){
            controlTask = iControlTaskService.getList(pageParam,Integer.parseInt(disposetype));
        }else
        {
            controlTask = iControlTaskService.getList(pageParam,0);
        }
        return successResult(controlTask);
    }
    @ApiOperation(value = "获取视频比对布控任务列表")
    @PostMapping(value = "/tasklistvideo")
    public CommonResult getTaskVideoList(@ApiParam(required = true, name = "pageParam", value = "{\n" +
            "  \"note\": {\n" +
            "  \"taskName\": \"任务名称\",\n" +
            "  \"createBy\":\"创建人\",\n" +
            "  },\n" +
            "  \"pageNo\": 1,\n" +
            "  \"pageSize\": 10,\n" +
            "}") @RequestBody PageParam pageParam, BindingResult bindingResult) {


        Page<ControlTask> controlTask = iControlTaskService.getList(pageParam,-1);


        return successResult(controlTask);
    }

    @PostMapping(value = "/inserttask")
    @ApiOperation(value = "新增任务")
    @NoRepeatSubmitAnnotation(prefix = "insertTask")
    public CommonResult insertTask(@ApiParam(required = true, name = "controlTask") @RequestBody @Valid TaskControlVO taskvo , BindingResult bindingResult) {
        this.validate(bindingResult);
        if (UtilValidate.isEmpty(taskvo)) {
            return failResult(SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code, SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.des, null);
        }
        return iControlTaskService.addControlTask(taskvo);
    }


    @PutMapping(value = "/updatetask")
    @ApiOperation(value = "修改任务：修改名称，处置类别，备注")
    public CommonResult updateTask(@ApiParam(required = true, name = "controlTask") @RequestBody   TaskControlVO taskvo ) {
        if (UtilValidate.isEmpty(taskvo)) {
            return failResult(SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code, SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.des, null);
        }
        return iControlTaskService.updateControlTask(taskvo);
    }


    @DeleteMapping("/deletetask/{taskId}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "删除布控任务")
    public CommonResult deleteTask(@PathVariable(value = "taskId") String taskId) {
        if (UtilValidate.isEmpty(taskId)) {
            return failResult("布控任务id不能为空!");
        }
        return iControlTaskService.deleteControlTask(taskId);
    }

    @GetMapping("/viewcontrol/{taskId}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "查看布控详细信息")
    public CommonResult GetControlTaskByID(@PathVariable("taskId") String taskId){
        if(UtilValidate.isEmpty(taskId))
        {
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "任务id不能为空！");
        }
        ControlTask controlTask= iControlTaskService.getById(taskId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id_control_task", taskId);
        List<ControlLibraryMid> listlibrarymid = iControlLibraryMidService.list(queryWrapper);
        List<String>listlib=new ArrayList<>();
        if(listlibrarymid!=null&&listlibrarymid.size()>0) {
            for (ControlLibraryMid clm : listlibrarymid) {
                listlib.add(clm.getIdFactory().toString());
            }
        }
        List<FaceCameraEntity> faceCameras = iControlBayonetMidService.selectFaceCameraBytaskID(taskId);
        controlTask.setListfacecamera(faceCameras);
        controlTask.setLibIds(listlib);

        return successResult(controlTask);

    }

    @GetMapping("/gettaskcamera/{taskno}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "根据任务获取摄像头列表")
    public CommonResult GetCameraListByTaskID(@PathVariable("taskno") String taskno){
        if(UtilValidate.isEmpty(taskno))
        {
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "任务id不能为空！");
        }
        return  iControlTaskService.getCameraListByTaskNO(taskno);
    }

    @DeleteMapping("/stopcontrol/{taskId}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "根据任务ID停止任务")
    public CommonResult StopControlByTaskID(@PathVariable("taskId") String taskId){
        if(UtilValidate.isEmpty(taskId))
        {
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "任务id不能为空！");
        }
        return  iControlTaskService.stopControlByTaskID(taskId);
    }

    @PostMapping("/suspendcontrol/{taskId}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "根据任务ID暂停任务")
    public CommonResult SuspendByTaskID(@PathVariable("taskId") String taskId){
        if(UtilValidate.isEmpty(taskId))
        {
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "任务id不能为空！");
        }
        return  iControlTaskService.suspendControlByTaskID(taskId);
    }
    @PostMapping("/resumecontrol/{taskId}")
    @ApiParam(required = true, name = "taskId", value = "")
    @ApiOperation(value = "根据任务ID恢复任务")
    public CommonResult resumeByTaskID(@PathVariable("taskId") String taskId){
        if(UtilValidate.isEmpty(taskId))
        {
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "任务id不能为空！");
        }
        return  iControlTaskService.resumeControlByTaskID(taskId);
    }

    @ApiOperation("上传人像附件布控")
    @PostMapping("/addtaskfaceFile")
    @NoRepeatSubmitAnnotation(prefix = "addtaskfaceFile")
    public CommonResult addtaskfaceFile(@RequestParam("file") MultipartFile multipartFile) {
        if(multipartFile.isEmpty()){
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "上传文件不能为空！");
        }
        return iControlTaskService.addtaskfaceFile(multipartFile);
    }

    @PostMapping(value = "/getImage")
    @ApiOperation(value = "上传 Image")
    public CommonResult oneToOneCompareImage1(
            @ApiParam(required = true, name = "compareRequestVo", value = "")
            @RequestBody CompareRequestVo compareRequestVo) {
        if (UtilValidate.isEmpty(compareRequestVo.getUrlList())) {
            throw new DkException("请选择需要比对的图片");
        }
        String fileName =  compareRequestVo.getUrlList().get(0);
        String s = compareService.setFaceCompare(fileName.substring(fileName.indexOf(",") + 1));
        return successResult(iControlTaskService.addtaskfaceFiles(s));
    }
    @ApiOperation("上传人像附件布控Base64")
    @PostMapping("/addtaskfaceFiles")
    @NoRepeatSubmitAnnotation(prefix = "addtaskfaceFiles")
    public CommonResult addtaskfaceFiles( @ApiParam(required = true, name = "FileImageVo", value = "")
                                          @RequestBody FileImageVo fileImageVo) {
        if(fileImageVo.getUrl().isEmpty()){
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "上传文件不能为空！");
        }
        return iControlTaskService.addtaskfaceFiles(fileImageVo.getUrl());
    }
    @ApiOperation("添加比对视频文件")
    @PostMapping("/addcomparvideoFile")
    @NoRepeatSubmitAnnotation(prefix = "addcomparvideoFile")
    public CommonResult addvideofaceFile(@ApiParam(required = true, name = "file", value = "视频文件")@RequestParam("file") MultipartFile multipartFile) {
        if(multipartFile.isEmpty()){
            return new CommonResult( null,SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code,  "上传文件不能为空！");
        }

        return iControlTaskService.addcomplevideoFile(multipartFile);
    }

    @ApiOperation("新增视频对比")
    @PostMapping("/insertcmpviedo")
    @RequiresPermissions("face:video:comparison")
    @NoRepeatSubmitAnnotation(prefix = "insertcmpviedo")
    public CommonResult addVideCompare(@ApiParam(required = true, name = "controlVO", value = "") @RequestBody @Valid VideoControlVo controlVO , BindingResult bindingResult) {

        this.validate(bindingResult);
        if (UtilValidate.isEmpty(controlVO)) {
            return failResult(SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.code, SystemCode.ILLEGAL_ARGUMENT_EXCEPTION.des, null);
        }
        return iControlTaskService.addcomplevideoTask(controlVO);


    }



}
